package com.uwlaser.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.domain.DataCourseParam;
import com.uwlaser.service.DataCourseParamService;
import com.uwlaser.service.dto.DataCourseParamDto;
import com.uwlaser.service.dto.DataCourseParamQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "过程数据管理")
@RequestMapping("/api/dataCourseParam")
public class DataCourseParamController {

    private final DataCourseParamService dataCourseParamService;

    @GetMapping
    @Log("查询过程数据")
    @ApiOperation("查询过程数据")
    @PreAuthorize("@el.check('dataCourseParam:list')")
    public ResponseEntity query(DataCourseParamQueryParam query, Pageable pageable){
        return new ResponseEntity<>(dataCourseParamService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增过程数据")
    @ApiOperation("新增过程数据")
    @PreAuthorize("@el.check('dataCourseParam:add')")
    public ResponseEntity create(@Validated @RequestBody DataCourseParamDto resources){
        return new ResponseEntity<>(dataCourseParamService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改过程数据")
    @ApiOperation("修改过程数据")
    @PreAuthorize("@el.check('dataCourseParam:edit')")
    public ResponseEntity update(@Validated @RequestBody DataCourseParamDto resources){
        dataCourseParamService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除过程数据")
    @ApiOperation("删除过程数据")
    @PreAuthorize("@el.check('dataCourseParam:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        dataCourseParamService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("导出过程数据")
    @ApiOperation("导出过程数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dataCourseParam:list')")
    public void download(HttpServletResponse response, DataCourseParamQueryParam query) throws IOException {
        dataCourseParamService.download(dataCourseParamService.queryAll(query), response);
    }

}
